package com.farm.bpm.form.impl;

import java.util.List;

import com.farm.bpm.form.Field;
import com.farm.bpm.form.FormParseInter;
import com.farm.parameter.domain.AloneDictionaryType;
import com.farm.parameter.service.DictionaryTypeManagerInter;
import com.farm.web.spring.BeanFactory;

public class DicTreeFormParse implements FormParseInter {
	private final static DictionaryTypeManagerInter aloneIMP = (DictionaryTypeManagerInter) BeanFactory
			.getBean("ALONE_DictionaryType_DAO_PROXY");

	@Override
	public void saveHandle(String fieldType, List<String> parameter,
			String name, Field field) {
		if (!"dictree".equals(fieldType)) {
			return;
		}
	}

	@Override
	public Field parse(String fieldType, List<String> parameter, String name,
			Field field) {
		if (!"dictree".equals(fieldType)) {
			return null;
		}
		AloneDictionaryType type = null;
		if (field.getValue() != null) {
			type = aloneIMP.getEntity(field.getValue());
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<select ");
		sb.append(" name='" + field.getName() + "'");
		sb.append(" id='" + field.getId() + "'");
		sb.append(" class='easyui-combotree'");
		sb.append(" style='width: " + field.getFormWidth() + "px;'");
		if (type != null) {
			sb.append(" val='" + type.getName() + "'");
		}
		sb
				.append("data-options=\"url:'admin/ALONEDictionaryTree_loadTree.do?index=");
		if (parameter != null && parameter.size() > 0) {
			sb.append(parameter.get(0));
		}
		sb.append("'");
		if (field.isRequired()) {
			sb.append(",required:false");
		}
		if (field.getValue() != null && type != null) {
			sb.append(",onlyLeaf:true,val:['" + field.getValue() + "','"
					+ type.getName() + "']");
		}
		sb.append("\"");
		sb.append(" >");
		sb.append("</select>");
		field.setFielsHtmlEdit(sb.toString());
		return field;
	}

	@Override
	public String getfieldTypeDescribes() {
		return "扩展类型:数据字典树形表单";
	}

	@Override
	public String getfieldTypeFormat() {
		return "name=\"名称#{'type':'dictree','parameter':['BPM_TEST_TREE']}\" type='string'";
	}

}
